package cn.fl.ref.contract.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.fl.framework.base.context.CurrentThreadContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.fl.ref.contract.service.IFncPrjContDetailApplyService;
import cn.fl.ref.contract.service.IFncPrjContDetailService;
import cn.fl.ref.contract.service.IFncPrjContMainApplyService;
import cn.fl.ref.contract.service.IFncPrjContMainService;
import cn.fl.ref.contract.vo.FncPrjContDetailApplyVO;
import cn.fl.ref.contract.vo.FncPrjContDetailVO;
import cn.fl.ref.contract.vo.FncPrjContMainApplyVO;
import cn.fl.ref.contract.vo.FncPrjContMainVO;
import cn.fl.ref.fncprjbscinfo.service.IFncPrjBscInfoService;
import cn.fl.ref.fncprjbscinfo.vo.FncPrjBscInfoVO;
import cn.fl.ref.fribaseinfo.transform.FriBaseFlowVOTransform;
import cn.fl.ref.fribaseinfo.vo.FriBaseFlowVO;
import cn.fl.system.condition.SysBkAccInfoQueryCondition;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysBkAccInfoService;
import cn.fl.system.vo.SysBkAccInfoVO;
import cn.fl.warrant.utils.JsonUtil;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ref.contract.condition.FncPrjContMainApplyQueryCondition;
import fl.ref.contract.condition.FncPrjContMainQueryCondition;
import fl.ref.contract.constant.FncPrjContMainConstant;
import fl.ref.crbaseinfo.constant.CrBaseInfoConstant;
import fl.ref.fncprjbscinfo.constant.FncPrjBaseInfoConstant;
import fl.ref.fribaseinfo.condition.FriBaseInfoQueryCondition;
import fl.ref.fribaseinfo.constant.FriBaseInfoConstant;
import fl.ref.fribaseinfo.dto.FriBaseFlowDTO;
import fl.warrant.facade.archive.constant.ArchiveConstants;

/**
 * @Description: 合同管理主表 控制层
 */
@Controller
@RequestMapping("/rfbuss/fncPrjContMain")
public class FncPrjContMainController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FncPrjContMainController.class);

	@Autowired
	private IFncPrjContMainService fncPrjContMainService;//合同服务
	@Autowired
	private IFncPrjContMainApplyService fncPrjContMainApplyService;//合同申请服务
	@Autowired
	private IFncPrjContDetailApplyService fncPrjContDetailApplyService;//合同明细服务
	@Autowired
	private IFncPrjContDetailService fncPrjContDetailService;//合同服务
	@Autowired
	IFncPrjBscInfoService fncPrjBscInfoService;//项目服务
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;//字典服务
	@Autowired
	private ISysBkAccInfoService sysBkAccInfoService;//狮桥支付银行账户

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("rfbuss/fncPrjContMain/fncPrjContMainList");
		String sysCd = CurrentThreadContext.getCurrentSysCd(); 
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		//获取项目状态
		List<KeyValueVO> prjStsList = this.fncPrjContMainService.getKvList(FncPrjContMainConstant.PRJ_STS_CD, sysCd, orgCd);
		view.addObject("prjStsList", prjStsList);
		//获取合同状态
		List<KeyValueVO> contStsList = this.fncPrjContMainService.getKvList(FncPrjContMainConstant.CONT_STS_CD, sysCd, orgCd);
		view.addObject("contStsList", contStsList);
		//获取盖章状态
		List<KeyValueVO> stampStsList = this.fncPrjContMainService.getKvList(FncPrjContMainConstant.STAMP_STS_CD, sysCd, orgCd);
		view.addObject("stampStsList", stampStsList);
		BaseLogUtils.info(logger, "toListPage", "跳转到合同管理列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("rfbuss/fncPrjContMain/modify");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String fncPrjNm = request.getParameter("fncPrjNm");//项目名称
		String fncPrjId = request.getParameter("fncPrjId");//项目编号
		String fundRasInsNm = request.getParameter("fundRasInsNm");//机构名称
		String prjStcd = request.getParameter("prjStcd");//项目状态
		String contStatus = request.getParameter("contStatus");//合同状态
		String stampStatus = request.getParameter("stampStatus");//盖章状态
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		FncPrjContMainQueryCondition qc = new FncPrjContMainQueryCondition();
		if (StringUtils.isNotEmpty(fncPrjNm)) {
			qc.setFncPrjNm(fncPrjNm);
		}
		if (StringUtils.isNotEmpty(fncPrjId)) {
			qc.setFncPrjId(fncPrjId);
		}
		if (StringUtils.isNotEmpty(fundRasInsNm)) {
			qc.setFundRasInsNm(fundRasInsNm);
		}
		if (StringUtils.isNotEmpty(prjStcd)) {
			qc.setFncPrjStcd(Integer.valueOf(prjStcd));
		}
		if (StringUtils.isNotEmpty(contStatus)) {
			qc.setContStatus(Integer.valueOf(contStatus));
		}
		if (StringUtils.isNotEmpty(stampStatus)) {
			qc.setStampStatus(Integer.valueOf(stampStatus));
		}
		page.setCondition(qc);

		Pager pager = this.fncPrjContMainService.searchListPage(page);
		List<FncPrjContMainVO> list = (List<FncPrjContMainVO>) pager.getResultList();
		ListPageVO<FncPrjContMainVO> listPage = new ListPageVO<FncPrjContMainVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.fncPrjContMainService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+id+"]", CurrentThreadContext.getCurrentUserName(), ex);
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			FncPrjContMainVO vo = this.fncPrjContMainService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]", CurrentThreadContext.getCurrentUserName(), ex);
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]", CurrentThreadContext.getCurrentUserName(), ex);
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		FncPrjContMainVO entity = null;
		try {
			entity = new FncPrjContMainVO();
			if(StringUtils.isNotEmpty(name)){
				//TODO ADD PARAM
			}
			Long id = this.fncPrjContMainService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(), ex);
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		FncPrjContMainVO entity = null;
		try {
			entity = new FncPrjContMainVO();
			entity.setId(Long.valueOf(id));
			//TODO ADD PARAM
			boolean success = this.fncPrjContMainService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName(), ex);
		}
		return result;
	}
	
	/**
	 * 发起合同审核流程
	 */
	@RequestMapping(value = "/addFlowStart.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addFlowStart(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String mainId = request.getParameter("mainId");
		String fncPrjId = request.getParameter("fncPrjId");
		String _contDetailList = request.getParameter("_contDetailList");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		FncPrjContMainQueryCondition qc = new FncPrjContMainQueryCondition();
		try {
			//1 校验合同主表主键
			if(StringUtils.isNotEmpty(mainId)){
				//2 新增合同申请主表数据与合同申请明细表数据  
				FncPrjContMainApplyVO mainApply = new  FncPrjContMainApplyVO();
				mainApply.setFncPrjId(fncPrjId);//融资项目基础信息表:融资项目编号（FNC_PRJ_BSC_INFO:FNC_PRJ_ID)
				mainApply.setFncPrjContMainId(Long.parseLong(mainId));//合同管理主表主键(FNC_PRJ_CONT_MAIN:ID)
				mainApply.setContNum(0l);//合同份数
				mainApply.setIsValid(ArchiveConstants.IS_VALID);//是否有效（1：是，0：否）（SYS0021）
				mainApply.setIsDel(ArchiveConstants.IS_DEL);//是否删除（1：是，0：否）
				mainApply.setCrtTm(new Date());//创建时间
				mainApply.setCrtUsrId(CurrentThreadContext.getCurrentUserId());//创建人ID
				Long mainApplyId=fncPrjContMainApplyService.add(mainApply);
				List<FncPrjContDetailApplyVO> detail= (List<FncPrjContDetailApplyVO>) JsonUtil.toList(_contDetailList, new FncPrjContDetailApplyVO());
				Long ContNum=0l;
				for(FncPrjContDetailApplyVO  detailVo: detail){
					detailVo.setFncPrjContMainApplyId(mainApplyId);
					detailVo.setIsValid(ArchiveConstants.IS_VALID);//是否有效（1：是，0：否）（SYS0021）
					detailVo.setIsDel(ArchiveConstants.IS_DEL);//是否删除（1：是，0：否）
					detailVo.setCrtTm(new Date());//创建时间
					detailVo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());//创建人ID
					detailVo.setFncPrjContDetailId(detailVo.getId());//合同明细主键id
					fncPrjContDetailApplyService.add(detailVo);
					ContNum++;
				}
				FncPrjContMainApplyVO umainApply = new  FncPrjContMainApplyVO();
				umainApply.setContNum(ContNum);
				umainApply.setId(mainApplyId);
				fncPrjContMainApplyService.modify(umainApply);//更新合同申请明细份数
				//3 发起流程
				qc.setId(mainApplyId);//合同申请主表ID
				qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
				qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
				qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());//当前登录人事业部代码
				qc.setUsrId(CurrentThreadContext.getCurrentUserId());
				qc.setUsrNm(CurrentThreadContext.getCurrentUserName());
				qc.setFlowModelType(FncPrjContMainConstant.FLOW_MODEL_TYP_CONT_APPROVAL_STAMP);//合同法审盖章审核流程
				//发起合同法审流程
				result = this.fncPrjContMainService.addFlowStart(qc);
				//4 更新状态
				if(BaseConstant.IS_YESNO_YES == result.getSuccess()){
					//更新合同申请明细表为审核中暂不考虑
					//更新合同明细表状态为审核中
					List<FncPrjContDetailVO> udetailList= (List<FncPrjContDetailVO>) JsonUtil.toList(_contDetailList, new FncPrjContDetailVO());
					for(FncPrjContDetailVO udetailVo:udetailList){
						if(StringUtils.isNotEmpty(udetailVo.getId())){
							FncPrjContDetailVO detailVo = new FncPrjContDetailVO();
							detailVo.setId(udetailVo.getId());
							detailVo.setContDetailStatus(1);//审核中
							detailVo.setMdfTm(new Date());
							detailVo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
							boolean success = this.fncPrjContDetailService.modify(detailVo);
							if(!success){
								result.setSuccess(BaseConstant.IS_YESNO_NO);
								result.setInfo("修改合同明细状态失败!");
								BaseLogUtils.info(logger, "addFlowStart", "修改合同申请主表状态失败.[detailId="+udetailVo.getId()+"] DATA:" + JSONObject.toJSONString(detailVo), CurrentThreadContext.getCurrentUserName());
							}	
						}
					}
				}
				result.setData(mainApplyId);
				BaseLogUtils.info(logger, "addFlowStart", "新增合同审核流程数据.[mainApplyId="+mainApplyId+"] DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName());
			}else{
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据错误：业务主键为空！");
				BaseLogUtils.info(logger, "addFlowStart", "新增合同审核流程数据失败.[mainApplyId="+mainId+"]", CurrentThreadContext.getCurrentUserName());
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "addFlowStart", "新增合同审核流程数据失败.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addFlowStart", "新增合同审核流程数据失败.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName(), ex);
		}

		return result;
	}
	
	/**
	 * 跳转到添加再融资合同信息页面
	 */
	@RequestMapping(value = "/toAddContPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddContPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("prjId");//项目id
		String from = request.getParameter("from");//合同法审节点标识
		ModelAndView view = new ModelAndView("rfbuss/fncPrjContMain/fncPrjContAdd");
		//判空-不为空进行信息查询
		if(StringUtils.isNotEmpty(id)){
			//查询项目
			FncPrjBscInfoVO fncPrjBscInfoVO = this.fncPrjBscInfoService.selectById(Long.parseLong(id));	
			this.setVOWithDicInfo(fncPrjBscInfoVO);
			view.addObject("fncPrjBscInfo", fncPrjBscInfoVO);
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("prjId", id);
			//查询合同
			if(null != fncPrjBscInfoVO){
				FncPrjContMainQueryCondition qc = new FncPrjContMainQueryCondition();
				qc.setFncPrjId(fncPrjBscInfoVO.getFncprjid());
				qc.setIsDel(0);//未删除
				qc.setIsValid(1);//有效
				List<FncPrjContMainVO> list = this.fncPrjContMainService.searchContMainList(qc);
				if(null != list && list.size()>0){
					FncPrjContMainVO fncPrjContMain = list.get(0);
					view.addObject("fncPrjContMain", fncPrjContMain);
					param.put("contId", fncPrjContMain.getId());
				}
			}
			//查询项目、授信
			Map<String, Object> prjScmCr = this.fncPrjContMainService.selectPrjScmCr(param);
			this.setDicInfo(prjScmCr);
			view.addObject("prjScmCr", prjScmCr);
		}
		view.addObject("from", from);
		BaseLogUtils.info(logger, "toAddContPage", "跳转到添加再融资合同信息页面.[prjId="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 再融资合同盖章申请页面
	 */
	@RequestMapping("/toStampFlowPage.do")
	public String toStampFlowPage(HttpServletRequest request,HttpServletResponse response) {
		String contId = request.getParameter("contId");//合同主表id
		String applyId = request.getParameter("applyId");//合同申请主表id
		
		if(StringUtils.isNotBlank(contId)) {
			//查询合同信息，关联项目、授信等信息
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("contId", contId);
			Map<String, Object> prjScmCr = fncPrjContMainService.selectPrjScmCr(param);
			
			this.setDicInfo(prjScmCr);
					
			request.setAttribute("prjScmCr", prjScmCr);
		}
		request.setAttribute("contId", contId);
		request.setAttribute("applyId", applyId);
		List<SysDictionaryDTO> pagefileList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), "REF0038");
		request.setAttribute("pagefileList", pagefileList);
		BaseLogUtils.info(logger, "toStampFlowPage", "再融资合同盖章申请页面.contId:" + contId, CurrentThreadContext.getCurrentUserName());
		request.setAttribute("sysDate", DateUtils.formatDateToString(new Date(), DateUtils.YYYY_MM_DD));
		request.setAttribute("loginUsrNm", CurrentThreadContext.getCurrentUserName());
		return "rfbuss/fncPrjContMain/fncPrjContStampApply";
	}
	
	/**
	 * 再融资合同盖章申请附加信息页面
	 */
	@RequestMapping("/toStampViewPage.do")
	public String toStampViewPage(HttpServletRequest request,HttpServletResponse response) {
		String contId = request.getParameter("contId");//合同主表id
		String flag = request.getParameter("flag");//流程任务节点标识
		String fromUrl="rfbuss/fncPrjContMain/fncPrjContStampView";
		if(StringUtils.isNotBlank(contId)) {
			//查询合同信息，关联项目、授信等信息
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("contId", contId);
			Map<String, Object> prjScmCr = fncPrjContMainService.selectPrjScmCr(param);
			if(prjScmCr!=null && !prjScmCr.isEmpty()){
			this.setDicInfo(prjScmCr);
					
			request.setAttribute("prjScmCr", prjScmCr);
		}
			//判断contId是来源于合同管理主表(373)合同管理申请主表(728)
			String str=String.valueOf(contId).substring(6, 9);
			boolean isDateFrom=true;//true 合同管理主表  false 合同管理申请主表
			if("728".equals(str)){
				isDateFrom=false;
			}
			if(StringUtils.isNotBlank(contId)&&!isDateFrom) {
				//根据合同申请主表Id获取信息
				FncPrjContMainApplyVO applyVo=fncPrjContMainApplyService.selectById(Long.parseLong(contId));
				if(applyVo.getFncPrjContMainId()!=null){
					if(StringUtils.isNotEmpty(applyVo.getFncPrjContMainId())){
						param.put("contId", applyVo.getFncPrjContMainId());
						Map<String, Object> prjScmCr1 = fncPrjContMainService.selectPrjScmCr(param);
						this.setDicInfo(prjScmCr1);
						request.setAttribute("prjScmCr", prjScmCr1);
					}
				}
				fromUrl="rfbuss/fncPrjContMain/fncPrjContStampViewN";
				request.setAttribute("applyId", contId);
			}
		}
		request.setAttribute("contId", contId);
		request.setAttribute("flag", flag);
		request.setAttribute("sysDate", DateUtils.formatDateToString(new Date(), DateUtils.YYYY_MM_DD));
		request.setAttribute("loginUsrNm", CurrentThreadContext.getCurrentUserName());
		request.setAttribute("from", fromUrl);
		BaseLogUtils.info(logger, "toStampViewPage", "再融资合同盖章申请附加信息页面.contId:" + contId, CurrentThreadContext.getCurrentUserName());
		return fromUrl;
	}
	
	/**
	 * 发起盖章审核流程
	 */
	@RequestMapping(value = "/addStampFlowStart.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addStampFlowStart(@RequestBody Object formdata, HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		FncPrjContMainQueryCondition qc = new FncPrjContMainQueryCondition();
		try {
			net.sf.json.JSONObject object = new net.sf.json.JSONObject();
			if(formdata != null){
				object = net.sf.json.JSONObject.fromObject(formdata);
				JSONArray grid1 = object.getJSONArray("griddata");
				String applyId = object.getString("applyId");
				String mainId = object.getString("mainId");
				String docArray = object.getString("docArray");//判断是否上传了盖章电子版附件文件
				List<FncPrjContDetailVO> detailLst = new ArrayList<FncPrjContDetailVO>();
				//判断主合同ID是否为空
				if(StringUtils.isNotEmpty(applyId)){
					//修改合同明细表 
					if(null != grid1){
					
						for(int i=0;i<grid1.size();i++){
							net.sf.json.JSONObject gobj = grid1.getJSONObject(i);
							if(gobj.has("ID") && null != gobj.getString("ID") && !"".equals(gobj.getString("ID"))){
								FncPrjContDetailVO detailVo = new FncPrjContDetailVO();
								detailVo.setId(Long.parseLong(gobj.getString("ID")));
								if(gobj.has("CONT_DETAIL_ARC_NUM")){
									String arcNum = gobj.getString("CONT_DETAIL_ARC_NUM");
									if(null != arcNum && !"".equals(arcNum)){
										detailVo.setContDetailArcNum(Long.parseLong(arcNum));
									}
								}
								//纸质合同归档
								if(gobj.has("paperContract")){
									if(null!=gobj.get("paperContract") && !"".equals(gobj.get("paperContract").toString())
											&& !"null".equals(gobj.get("paperContract").toString())
											&& !"0".equals(gobj.get("paperContract").toString())){
										detailVo.setPaperContract(Integer.parseInt(gobj.get("paperContract").toString()));
									}else{
										detailVo.setPaperContract(0);
									}
								}else{
									detailVo.setPaperContract(0);
								}
								//盖章电子版附件是否存在
								if(docArray!=null && docArray!= "[]"){
									detailVo.setStampeFile(1);//1 存在
								}else {
									detailVo.setStampeFile(0);//1 存在
								}
								detailVo.setMdfTm(new Date());
								detailVo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
								detailLst.add(detailVo);
							}else{
								continue;
							}
						}
						boolean flag = this.fncPrjContDetailService.batchModifyDetailInfo(detailLst);
						if(!flag){
							result.setSuccess(BaseConstant.IS_YESNO_NO);
							result.setInfo("修改合同明细失败!");
							BaseLogUtils.info(logger, "addStampFlowStart", "修改合同明细失败.[applyId="+applyId+"] DATA:" + JSONObject.toJSONString(detailLst), CurrentThreadContext.getCurrentUserName());
							return result;
						}
					}else{
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("列表参数为空！修改合同明细失败!");
						BaseLogUtils.info(logger, "addStampFlowStart", "列表参数为空！修改合同明细失败.[applyId="+applyId+"]", CurrentThreadContext.getCurrentUserName());
						return result;
					}
					
					//设置流程参数
					qc.setId(Long.parseLong(applyId));//合同申请主表ID
					qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
					qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
					qc.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());//当前登录人事业部代码
					qc.setUsrId(CurrentThreadContext.getCurrentUserId());
					qc.setUsrNm(CurrentThreadContext.getCurrentUserName());
					qc.setFlowModelType(FncPrjContMainConstant.FLOW_MODEL_TYP_CONT_STAMP);//"R016"盖章审核
					//发起盖章流程
					result = this.fncPrjContMainService.addFlowStart(qc);
					//if(BaseConstant.IS_YESNO_YES == result.getSuccess()){
						FncPrjContMainVO entity = new FncPrjContMainVO();
						entity.setId(Long.parseLong(mainId));
						entity.setStampStatus(1);//审核中
						entity.setMdfTm(new Date());
						entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
						boolean success = this.fncPrjContMainService.modify(entity);
						if(!success){
							result.setSuccess(BaseConstant.IS_YESNO_NO);
							result.setInfo("修改盖章状态失败!");
							BaseLogUtils.info(logger, "addStampFlowStart", "修改盖章状态失败.[applyId="+applyId+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
						}
						//修改合同明细盖章状态
						for(FncPrjContDetailVO udetailVo:detailLst){
							if(StringUtils.isNotEmpty(udetailVo.getId())){
								FncPrjContDetailVO detailVo = new FncPrjContDetailVO();
								detailVo.setId(udetailVo.getId());
								detailVo.setStampStatus(1);//审核中
								detailVo.setMdfTm(new Date());
								detailVo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
								boolean successd = this.fncPrjContDetailService.modify(detailVo);
								if(!successd){
									result.setSuccess(BaseConstant.IS_YESNO_NO);
									result.setInfo("修改合同明细盖章状态失败!");
									BaseLogUtils.info(logger, "addStampFlowStart", "修改合同明细盖章状态失败.[detailId="+udetailVo.getId()+"] DATA:" + JSONObject.toJSONString(detailVo), CurrentThreadContext.getCurrentUserName());
								}	
							}
						}
					//}
					result.setData(Long.parseLong(applyId));
					BaseLogUtils.info(logger, "addStampFlowStart", "新增盖章审核流程数据.[applyId="+applyId+"] DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName());
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("数据错误：业务主键为空！");
					BaseLogUtils.info(logger, "addStampFlowStart", "新增盖章审核流程数据失败1.[applyId="+applyId+"]", CurrentThreadContext.getCurrentUserName());
				}
			}else{
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("列表参数为空！修改合同明细失败!");
				BaseLogUtils.info(logger, "addStampFlowStart", "列表参数为空！修改合同明细失败.[formdata=" + JSONObject.toJSONString(formdata) + "]", CurrentThreadContext.getCurrentUserName());
				return result;
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "addStampFlowStart", "新增盖章审核流程数据失败2.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addStampFlowStart", "新增盖章审核流程数据失败3.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName(), ex);
		}

		return result;
	}
	
	/**
	 * 盖章审核流程附加信息页面-保存合同明细信息
	 */
	@RequestMapping(value = "/saveContDetail.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object saveContDetail(@RequestBody Object formdata, HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "数据保存成功！");
		try {
			net.sf.json.JSONObject object = new net.sf.json.JSONObject();
			if(formdata != null){
				object = net.sf.json.JSONObject.fromObject(formdata);
				JSONArray grid1 = object.getJSONArray("griddata");
				String mainId = object.getString("mainId");
				String docArray = object.getString("docArray");//判断是否上传了盖章电子版附件文件
				//判断主合同ID是否为空
				if(StringUtils.isNotEmpty(mainId)){
					//修改合同明细表 
					if(null != grid1){
						List<FncPrjContDetailVO> detailLst = new ArrayList<FncPrjContDetailVO>();
						for(int i=0;i<grid1.size();i++){
							net.sf.json.JSONObject gobj = grid1.getJSONObject(i);
							if(gobj.has("ID") && null != gobj.getString("ID") && !"".equals(gobj.getString("ID"))){
								FncPrjContDetailVO detailVo = new FncPrjContDetailVO();
								detailVo.setId(Long.parseLong(gobj.getString("ID")));
								if(gobj.has("CONT_DETAIL_ARC_NUM")){
									String arcNum = gobj.getString("CONT_DETAIL_ARC_NUM");
									if(null != arcNum && !"".equals(arcNum)){
										detailVo.setContDetailArcNum(Long.parseLong(arcNum));
									}
								}
								if(gobj.has("CONT_DETAIL_ARC_REALNUM")){
									String arcRealNum = gobj.getString("CONT_DETAIL_ARC_REALNUM");
									if(null != arcRealNum && !"".equals(arcRealNum)){
										detailVo.setContDetailArcRealnum(Long.parseLong(arcRealNum));
									}
								}
								//纸质合同归档
								if(gobj.has("paperContract")){
									if(null!=gobj.get("paperContract") && !"".equals(gobj.get("paperContract").toString())
											&& !"null".equals(gobj.get("paperContract").toString()) &&
													!"0".equals(gobj.get("paperContract").toString())){
										detailVo.setPaperContract(Integer.parseInt(gobj.get("paperContract").toString()));
									}else{
										detailVo.setPaperContract(0);
									}
								}else{
									detailVo.setPaperContract(0);
								}
								//盖章电子版附件是否存在
								if(docArray==null || "[]".equals(docArray)){
									detailVo.setStampeFile(0);//0不存在
								}else {
									detailVo.setStampeFile(1);//1 存在
								}
								detailVo.setMdfTm(new Date());
								detailVo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
								detailLst.add(detailVo);
							}else{
								continue;
							}
						}
						boolean flag = this.fncPrjContDetailService.batchModifyDetailInfo(detailLst);
						if(!flag){
							result.setSuccess(BaseConstant.IS_YESNO_NO);
							result.setInfo("修改合同明细失败!");
							BaseLogUtils.info(logger, "saveContDetail", "修改合同明细失败.[mainId="+mainId+"] DATA:" + JSONObject.toJSONString(detailLst), CurrentThreadContext.getCurrentUserName());
							return result;
						}
					}else{
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("列表参数为空！修改合同明细失败!");
						BaseLogUtils.info(logger, "saveContDetail", "列表参数为空！修改合同明细失败.[mainId="+mainId+"]", CurrentThreadContext.getCurrentUserName());
						return result;
					}
					result.setData(Long.parseLong(mainId));
					BaseLogUtils.info(logger, "saveContDetail", "修改合同明细数据.[mainId="+mainId+"]", CurrentThreadContext.getCurrentUserName());
				}else{
					result.setSuccess(BaseConstant.IS_YESNO_NO);
					result.setInfo("数据错误：业务主键为空！");
					BaseLogUtils.info(logger, "saveContDetail", "修改合同明细数据失败1.[mainId="+mainId+"]", CurrentThreadContext.getCurrentUserName());
				}
			}else{
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("列表参数为空！修改合同明细失败!");
				BaseLogUtils.info(logger, "saveContDetail", "列表参数为空！修改合同明细失败.[formdata=" + JSONObject.toJSONString(formdata) + "]", CurrentThreadContext.getCurrentUserName());
				return result;
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "saveContDetail", "修改合同明细数据失败2.", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "saveContDetail", "修改合同明细数据失败3.", CurrentThreadContext.getCurrentUserName(), ex);
		}

		return result;
	}
	
	/**
	 * 设置字典项信息
	 * @param VO
	 */
	private void setDicInfo(Map<String, Object> prjScmCr) {
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		
		Map<String, String> bustypelist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FncPrjBaseInfoConstant.FNC_PRJ_BSC_INFO_BUS_TYPE);
		
		// 业务类型
		if (bustypelist.containsKey(prjScmCr.get("BUS_TYPE"))) {
			prjScmCr.put("BUS_TYPE_",bustypelist.get(prjScmCr.get("BUS_TYPE")));
		}
		
		// 首付款计算
		BigDecimal FSTPY_AMT = new BigDecimal(0);
		if(prjScmCr.get("FSTPY_AMT") != null) {
			FSTPY_AMT = (BigDecimal)prjScmCr.get("FSTPY_AMT");
		}
		
		BigDecimal FSTPY_DEP_ADD = new BigDecimal(0);
		try {
			if(prjScmCr.get("FSTPY_DEP_ADD") != null){
				FSTPY_DEP_ADD = new BigDecimal((String)prjScmCr.get("FSTPY_DEP_ADD"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			BaseLogUtils.error(logger, "setDicInfo", "再融资合同审核流程附加页面：FSTPY_DEP_ADD无法转成BigDecimal.", CurrentThreadContext.getCurrentUserName(), e);
		}
		
		prjScmCr.put("firstPayment",FSTPY_AMT.add(FSTPY_DEP_ADD));
		
	}
	
	/**
	 * 设置产品基本信息中关于字典项信息
	 * @param VO
	 */
	private void setVOWithDicInfo(FncPrjBscInfoVO vo) {
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		Map<String, String> countrylist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FncPrjBaseInfoConstant.FNC_PRJ_BSC_INFO_CTY);
		Map<String, String> bustypelist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FncPrjBaseInfoConstant.FNC_PRJ_BSC_INFO_BUS_TYPE);
		Map<String, String> firtypelist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FriBaseInfoConstant.FUND_RAS_INS_BASE_INFO_INS_TYPE_CD);
		Map<String, String> crsbjlist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CrBaseInfoConstant.CR_BSC_INFO_CR_SBJ);
		Map<String, String> stcdlist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FncPrjBaseInfoConstant.FNC_PRJ_BSC_INFO_ST_CD);
		Map<String, String> transstlist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FncPrjBaseInfoConstant.FNC_PRJ_TRANS_ST_CD);
		Map<String, String> astIdlist = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, FncPrjBaseInfoConstant.FNC_PRJ_AST_ID);
		SysBkAccInfoQueryCondition qc = new SysBkAccInfoQueryCondition();
		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		List<SysBkAccInfoVO> bkAccList = sysBkAccInfoService.searchList(qc);
		if(null != vo){
			// 到账收款账户
			if (vo.getTransacc() != null) {
				for (SysBkAccInfoVO sysBkAccInfoVO : bkAccList) {
					if (sysBkAccInfoVO.getId().toString().equals(vo.getTransacc())) {
						vo.setTransaccNm(sysBkAccInfoVO.getAccNo());
						break;
					}
				}
			}
	
			// 预放款收款账户
			if (vo.getExpwdacc() != null) {
				for (SysBkAccInfoVO sysBkAccInfoVO : bkAccList) {
					if (sysBkAccInfoVO.getId().toString().equals(vo.getExpwdacc())) {
						vo.setExpwdaccNM(sysBkAccInfoVO.getAccNo());
						break;
					}
				}
			}
	
			// 审核状态
			if (stcdlist.containsKey(vo.getFncprjstcd() + "")) {
				vo.setFncprjstNM(stcdlist.get(vo.getFncprjstcd() + ""));
			}
	
			// 到账状态
			if (transstlist.containsKey(vo.getTransst() + "")) {
				vo.setTransstNM(transstlist.get(vo.getTransst() + ""));
			}
			// 国家
			if (countrylist.containsKey(vo.getCountry() + "")) {
				vo.setCountryNM(countrylist.get(vo.getCountry() + ""));
			}
			// 业务类型
			if (bustypelist.containsKey(vo.getBustype() + "")) {
				vo.setBustypeNM(bustypelist.get(vo.getBustype() + ""));
			}
			// 融资机构类型
			if (firtypelist.containsKey(vo.getInstypecd() + "")) {
				vo.setInstypeNM(firtypelist.get(vo.getInstypecd() + ""));
			}
			// 资产占用种类
			if (astIdlist.containsKey(vo.getAstId())) {
				vo.setAstNm(astIdlist.get(vo.getAstId()));
			}
	
			// 授信主体
			if (crsbjlist.containsKey(vo.getCrsbj() + "")) {
				vo.setCrsbjNM(crsbjlist.get(vo.getCrsbj() + ""));
			}
			// 首付款
			if (vo.getFstpyamt() == null) {
				vo.setFstpyamt(0L);
			}
			if (vo.getFstpydepadd() != null) {
				vo.setFstpyamtall(vo.getFstpyamt() + Long.parseLong(vo.getFstpydepadd()));
			} else {
				vo.setFstpyamtall(vo.getFstpyamt());
			}
		}
	}
	/**
	 * 到合同相关的流程信息页面 kfc
	 */
	@RequestMapping(value = "/toContFlow.do", method = { RequestMethod.GET })
	public ModelAndView toContFlow(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("/rfbuss/fncPrjContMain/fncPrjContFlow");
		view.addObject("id", request.getParameter("id"));
		return view;
	}
	/**
	 * 查询合同相关的流程信息 kfc
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initContFlowList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initContFlowList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");

		String id = request.getParameter("id");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		FriBaseInfoQueryCondition condition = new FriBaseInfoQueryCondition();

		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		condition.setId(Long.valueOf(id));

		page.setCondition(condition);

		Pager pager = this.fncPrjContMainService.searchFlowPageById(page);
		List<FriBaseFlowDTO> list = (List<FriBaseFlowDTO>) pager.getResultList();
		List<FriBaseFlowVO> voList = FriBaseFlowVOTransform.toVOList(list);
		BaseLogUtils.info(logger, "voList", "查询数据完成. voList:" + JSONObject.toJSONString(voList), CurrentThreadContext.getCurrentUserName());
		//add  合同法审流程主键来源于合同管理申请主表
		//根据合同管理主键获取合同管理申请信息
		FncPrjContMainApplyQueryCondition applyQc=new FncPrjContMainApplyQueryCondition();
		applyQc.setFncPrjContMainId(Long.parseLong(id));
		List<FncPrjContMainApplyVO> applylist=fncPrjContMainApplyService.searchContMainList(applyQc);
		if(applylist!=null && applylist.size()>0){
			for(FncPrjContMainApplyVO applyVo:applylist){
				condition.setId(Long.valueOf(applyVo.getId()));
				page.setCondition(condition);
				Pager pager2 = this.fncPrjContMainService.searchFlowPageById(page);
				List<FriBaseFlowDTO> list2 = (List<FriBaseFlowDTO>) pager2.getResultList();
				List<FriBaseFlowVO> voList2 = FriBaseFlowVOTransform.toVOList(list2);
				voList.addAll(voList2);
			}
		}//end by gengchunlan 20180521
		ListPageVO<FriBaseFlowVO> listPage = new ListPageVO<FriBaseFlowVO>(voList, voList.size());
		BaseLogUtils.info(logger, "initContFlowList", "查询数据完成. DATA:" + JSONObject.toJSONString(voList), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	/**
	 * 跳转到查看再融资合同信息页面
	 */
	@RequestMapping(value = "/toContractView.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toContractView(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("prjId");//项目id
		String from = request.getParameter("from");//合同法审节点标识
		ModelAndView view = new ModelAndView("rfbuss/fncPrjContMain/fncPrjContView");
		//判空-不为空进行信息查询
		if(StringUtils.isNotEmpty(id)){
			//查询项目
			FncPrjBscInfoVO fncPrjBscInfoVO = this.fncPrjBscInfoService.selectById(Long.parseLong(id));	
			this.setVOWithDicInfo(fncPrjBscInfoVO);
			view.addObject("fncPrjBscInfo", fncPrjBscInfoVO);
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("prjId", id);
			//查询合同
			if(null != fncPrjBscInfoVO){
				FncPrjContMainQueryCondition qc = new FncPrjContMainQueryCondition();
				qc.setFncPrjId(fncPrjBscInfoVO.getFncprjid());
				qc.setIsDel(0);//未删除
				qc.setIsValid(1);//有效
				List<FncPrjContMainVO> list = this.fncPrjContMainService.searchContMainList(qc);
				if(null != list && list.size()>0){
					FncPrjContMainVO fncPrjContMain = list.get(0);
					view.addObject("fncPrjContMain", fncPrjContMain);
					param.put("contId", fncPrjContMain.getId());
					//获取合同管理申请主表list
					FncPrjContMainApplyQueryCondition mainQc = new FncPrjContMainApplyQueryCondition();
					mainQc.setFncPrjContMainId(fncPrjContMain.getId());
					List<FncPrjContMainApplyVO> applyMainList=this.fncPrjContMainApplyService.searchContMainList(mainQc);
					List<Map<String,Object>> applyIds=new ArrayList<Map<String,Object>>();//封装 合同申请主表ids
					for(FncPrjContMainApplyVO mainApply :applyMainList){
						if(mainApply!=null && mainApply.getId()!=null){
							Map<String,Object> applyMap=new HashMap<String,Object>();
							//Map 
							applyMap.put("id", mainApply.getId());
							Pager page = new Pager();
							FriBaseInfoQueryCondition condition = new FriBaseInfoQueryCondition();
							condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
							condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
							condition.setId(mainApply.getId());
							page.setCondition(condition);

							Pager pager = this.fncPrjContMainService.searchFlowPageById(page);
							List<FriBaseFlowDTO> plist = (List<FriBaseFlowDTO>) pager.getResultList();
							List<FriBaseFlowVO> voList = FriBaseFlowVOTransform.toVOList(plist);
							//根据业务主键
							applyMap.put("processVoList",voList );//流程号
							applyIds.add(applyMap);
						}
					}
					view.addObject("applyIdList", JSONArray.fromObject(applyIds));
				}
			}
			//查询项目、授信
			Map<String, Object> prjScmCr = this.fncPrjContMainService.selectPrjScmCr(param);
			this.setDicInfo(prjScmCr);
			view.addObject("prjScmCr", prjScmCr);
		}
		view.addObject("from", from);
		BaseLogUtils.info(logger, "toContractView", "跳转到添加再融资合同信息页面.[prjId="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 跳转到添加再融资合同信息修改页面
	 */
	@RequestMapping(value = "/toModifyFncPrjCont.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyFncPrjCont(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("prjId");//项目id
		String from = request.getParameter("from");//合同法审节点标识
		String applyId = request.getParameter("applyId");//合同法审节点标识
		ModelAndView view = new ModelAndView("rfbuss/fncPrjContMain/fncPrjContModify");
		//判空-不为空进行信息查询
		if(StringUtils.isNotEmpty(id)){
			//查询项目
			FncPrjBscInfoVO fncPrjBscInfoVO = this.fncPrjBscInfoService.selectById(Long.parseLong(id));	
			this.setVOWithDicInfo(fncPrjBscInfoVO);
			view.addObject("fncPrjBscInfo", fncPrjBscInfoVO);
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("prjId", id);
			//查询合同
			if(null != fncPrjBscInfoVO){
				FncPrjContMainQueryCondition qc = new FncPrjContMainQueryCondition();
				qc.setFncPrjId(fncPrjBscInfoVO.getFncprjid());
				qc.setIsDel(0);//未删除
				qc.setIsValid(1);//有效
				List<FncPrjContMainVO> list = this.fncPrjContMainService.searchContMainList(qc);
				if(null != list && list.size()>0){
					FncPrjContMainVO fncPrjContMain = list.get(0);
					view.addObject("fncPrjContMain", fncPrjContMain);
					param.put("contId", fncPrjContMain.getId());
				}
			}
			//查询项目、授信
			Map<String, Object> prjScmCr = this.fncPrjContMainService.selectPrjScmCr(param);
			this.setDicInfo(prjScmCr);
			view.addObject("prjScmCr", prjScmCr);
		}
		view.addObject("from", from);
		view.addObject("applyId", applyId);
		BaseLogUtils.info(logger, "toModifyFncPrjCont", "跳转到添加再融资合同信息修改页面.[prjId="+id+"]", CurrentThreadContext.getCurrentUserName());
		return view;
	}
}

